<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="CSI.Directory.Core.Model"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
 <title>Manage Directory</title>
    <link href="../../Assets/CSS/mstyle.css" rel="stylesheet" type="text/css" />
    
    <script type="text/javascript">
   
    
    </script>
</head>
<body  style="background-color:#000">
    
    <div id="nav">
    <h2>Manage Directory</h2><br />
    <ul>
        <li class="inactive"><a href="/Directory/Manage">Listings</a></li>
        <li class="inactive"><a href="/Directory/Manage/Categories">Categories</a></li>
        <li>Features</li>
    </ul>
    
    <div id="subNav">
        Featured Listings
        <div id="main">
            <b>Add a Feature</b>
            <table>
                <tr>
                    <td>Listing:
                    </td>
                    <td>
                        <select id="listings" >
                            
                        <%foreach (var l in ((IList<Listing>)ViewData["Listings"]).OrderBy(t=>t.Name))
                         {%>
                             <option value="<%=l.Id %>"><%=l.Name%></option>
                         <% }%> 
                            
                        </select>
                    </td>
                </tr>
                <tr>
                    <td> Category:
                    </td>
                    <td> 
                        <select id="categories" >
                            
                        <%foreach (var c in (IList<Category>)ViewData["Categories"])
                         {%>
                             <option value="<%=c.Id %>"><%=c.Name%></option>
                         <% }%> 
                            
                        </select>
                    </td>
                </tr>
                
            </table>
           <a href="" onclick="AddFeature(); return false;">Save</a>
             <br />           
           

            <%if (ViewData["FeaturedListings"]!=null) {%>
            <br /><br /><b>Manage Features</b><br /><br />
            
            <%foreach (var l in (IList<Listing>)ViewData["FeaturedListings"])
             {%>
                 <%=l.Name%>
                 <blockquote>
                 <%foreach (var lc in l.Categories)
                 {%>
                    
                     <a href="" onclick="RemoveFeaturedListing(<%=lc.Id %>); return false;">remove</a>&nbsp;<%=((IList<Category>)ViewData["Categories"]).Single(t=>t.Id == lc.Category).Name%><br />
                 <% }%> 
             
                 </blockquote>
             <% }%> 
             
           <% }%> 
              
        <div id="error"></div>
        </div>
    </div>
    </div>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="../../Assets/Scripts/jquery.toJson.js" type="text/javascript"></script>
<script type="text/javascript">
var data = new Array();
function AddFeature()
{

    data = { 
                Listing: $("#listings").val(),
                Category: $("#categories").val()
                
              }; 

     var encoded = $.toJSON(data);
     $.ajax({
                url:"/Directory/Manage/AddFeature", 
                type: "POST",
                dataType: 'json',
                data: encoded,
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    if(data.Successful)
                        window.location.reload();
                    else
                        $("#error").html(data.Exception); 
                }
            });

}
function RemoveFeaturedListing(id)
{

    data = { 
                Id: id
                                
           }; 

     var encoded = $.toJSON(data);
     $.ajax({
                url:"/Directory/Manage/RemoveFeature", 
                type: "POST",
                dataType: 'json',
                data: encoded,
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    if(data.Successful)
                        window.location.reload();
                    else
                        $("#error").html(data.Exception); 
                }
            });

}
</script>
</body>
</html>
